/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.ioc.control.PluginController;

import fever.ioc.events.PluginEvent;
import fever.ioc.manager.controls.ActivatePlugin;
import fever.ioc.manager.controls.RegisterPlugin;
import fever.ioc.manager.events.PluginManagerEventList;
import fever.ioc.manager.events.PluginManagerListener;
import fever.ioc.manager.models.PluginManagerModel;
import fever.ioc.plugin.FeverPlugin;

/**
 * Abstract Plugin manager implementation.
 * 
 * <p>Manager plugin is responsible of others plugins registration.<br />
 * When a {@link fever.ioc.plugin.RegistredPlugin} plugin is loaded in 
 * the context, Manager know it.<br />
 * Manager can so activate / desactivate them at runtime.
 * 
 * <p>All model side ( logic ) of manager is already implemented, you 
 * have just to implements view side using 
 * {@link fever.ioc.manager.events.PluginManagerListener} interface.
 * 
 * //TODO Basic sample application.
 * 
 * @author Romain Ecarnot
 */
class fever.ioc.manager.AbstractManagerPlugin extends FeverPlugin
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _model : PluginManagerModel;
	private var _view : PluginManagerListener;
	private var _bAvailable : Boolean;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Triggered by all loaded plugins in context.
	 * 
	 * @param event {@link fever.ioc.events.PluginEvent}
	 */
	public function onRegisterPlugin( event : PluginEvent ) : Void
	{
		firePrivateEvent( event );
	}
	
	/**
	 * Plugin is ready to be used.
	 */
	public function run(  ) : Void
	{
		var controller : PluginController = getController();
		
		controller.pushCommandClass( 
			PluginManagerEventList.onRegisterPluginEVENT, 
			RegisterPlugin 
		);
		controller.pushCommandClass( 
			PluginManagerEventList.onActivatePluginEVENT, 
			ActivatePlugin 
		);
		
		_model = new PluginManagerModel( this );
		
		_bAvailable = true;
	}
	
	/**
	 * Returns {@code true} is manager is ready.
	 */
	public function isManagerAvailable(  ) : Boolean
	{
		return _bAvailable;	
	}
	
	/**
	 * Returns plugin manager view.
	 */
	public function getManagerView(  ) : PluginManagerListener
	{
		return _view;
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
		
	/**
	 * Constructor.
	 * 
	 * @param loadConfig To enable configuration loading or not.
	 */ 
	private function AbstractManagerPlugin( loadConfig : Boolean ) 
	{
		super( loadConfig );
	}	
}